package com.tongda.menu.adapter;

import java.util.List;

import com.tongda.core.vo.DishesCategory;
import com.tongda.menu.R;
import com.tongda.menu.R.drawable;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class DishCategoryAdapter extends BaseAdapter{

	private Context context;
	private List<DishesCategory> datas;
	private int currentPosition = 0;
	
	public DishCategoryAdapter(Context context, List<DishesCategory> datas) {
			this.context = context;
			this.datas = datas;
}
	
	public class Holder{
		TextView dishcyItem;
		TextView dishcyRed;
	}

	
	/**
	 * 将菜类的名字每个字符换行显示
	 * @param dcName(菜类的名字)
	 */
	private String brString(String dcName){
		String result = "";
		char[] chNames = dcName.toCharArray();
		StringBuilder sb = new StringBuilder("");
		for(char c:chNames){
			sb.append(c).append("\n");
		}
		result = sb.toString();
		return result;
	}

	public int getCurrentPosition() {
		return currentPosition;
	}

	public void setCurrentPosition(int currentPosition) {
		this.currentPosition = currentPosition;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		if(convertView==null){
			holder = new Holder();
			convertView = View.inflate(context, R.layout.dishcyitem, null);
			holder.dishcyItem = (TextView) convertView.findViewById(R.id.dishcy_item);
			holder.dishcyRed =(TextView)convertView.findViewById(R.id.dishcy_item_red);
			convertView.setTag(holder);
		}else {
			holder = (Holder) convertView.getTag();
		}
		if(datas.size()==0){
			return convertView;
		}
		DishesCategory dc = datas.get(position);
		String dcName = dc.getCategoryName();
		String result = brString(dcName);
		holder.dishcyItem.setText(result);
		
		if(currentPosition==position){
			convertView.setBackgroundResource(drawable.dc_selected);
			holder.dishcyItem.setTextColor(context.getResources().getColor(R.color.white));
			//动态的增加该item的宽度
		}else{
			convertView.setBackgroundResource(drawable.dc_unselected);
			holder.dishcyItem.setTextColor(context.getResources().getColor(R.color.nearwhite));
		}
		int num = dc.getDishNum();
		if(num>0 && !dc.getCategoryId().equals("zpc")&& !dc.getCategoryId().equals("tjc")&& !dc.getCategoryId().equals("tjcy")&& !dc.getCategoryId().equals("hytj")){
			holder.dishcyRed.setVisibility(View.VISIBLE);
			holder.dishcyRed.setText(String.valueOf(datas.get(position).getDishNum()));
		}else{
			holder.dishcyRed.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}

	public List<DishesCategory> getDatas() {
		return datas;
	}

	public void setDatas(List<DishesCategory> datas) {
		this.datas = datas;
		notifyDataSetChanged();
	}

}
